package bar.dark.lee.widget;


import java.math.BigDecimal;
import java.util.Calendar;

import bar.dark.lee.BDConstents;
import bar.dark.lee.IntroActivitiy;
import bar.dark.lee.IntroPasswordActivity;
import bar.dark.lee.R;
import bar.dark.lee.common.BDUtil;
import bar.dark.lee.common.DBSQLite;
import bar.dark.lee.common.DBSQLite.ParseSelectData;
import android.app.Activity;
import android.app.PendingIntent;
import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.os.IBinder;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RemoteViews;
import android.widget.TextView;

public class WidgetService2 extends Service
{
	// 0 = 총자산, 1 = 총통장, 2 = 현금, 3 = 카드대금 
	// 4 = 이달의지출, 5 = 이달의카드, 6 = 이달의수입
	// 7 = 오늘의지출, 8 = 오늘의카드, 9 = 오늘의수입
	// 10 = 통장, 11 = 체크카드, 12 = 신용카드
	
	@Override
	public void onStart(Intent intent, int startId)
	{
		super.onStart(intent, startId);
		
//		if (mContext == null)
//		{
//			Log.e("accountBook Total Money : ", "mContext = " + mContext);
//			removeAlarm();
//			return;
//		}
		
		setDataUpdate(getBaseContext());
	}
	
	@Override
	public IBinder onBind(Intent intent)
	{
		return null;
	}

	public void setDataUpdate(Context context)
	{
		SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
		String shape = pref.getString(BDConstents.CURRENCY_SHAPE, "원");
		shape = " " + shape;
		
		String clearKey = pref.getString(BDConstents.WIDGET_CLEAR_SETTING, "0");
		
		WidgetGetSqlQuery query = new WidgetGetSqlQuery(context);
		
		int layoutId = query.getSmallLayout(clearKey, 0);
		
		RemoteViews remoteView = new RemoteViews(context.getPackageName(), layoutId);
		
		String settingKey = pref.getString(BDConstents.SMALL_WIDGET_SETTING, "4,5");
		
		String [] key = settingKey.split(",");
		
		if (key.length < 2 )
		{
			settingKey = "4,5";
			key = settingKey.split(",");
		}
		
		String money = "";
		
		money = query.getMoney(key[0], 4, 0);
		
    	remoteView.setTextViewText(query.getSmallMoneyId(clearKey, 1), money + shape);
    	remoteView.setTextColor(query.getSmallMoneyId(clearKey, 1), query.getColor(key[0]));
    	remoteView.setTextViewText(query.getSmallTextViewId(clearKey, 1), query.getName(key[0], 4, 0));
    	
    	money = query.getMoney(key[1], 5, 0);
    	
    	remoteView.setTextViewText(query.getSmallMoneyId(clearKey, 2), money + shape);
    	remoteView.setTextColor(query.getSmallMoneyId(clearKey, 2), query.getColor(key[1]));
    	remoteView.setTextViewText(query.getSmallTextViewId(clearKey, 2), query.getName(key[1], 5, 0));
    	
		Intent intent = new Intent(context, IntroPasswordActivity.class);
		PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
		
		remoteView.setOnClickPendingIntent(R.id.widgetView2, pendingIntent);
		
		AppWidgetManager manager = AppWidgetManager.getInstance(context);
		ComponentName componentName = new ComponentName(context, AccountBookWidget2.class);
		manager.updateAppWidget(componentName, remoteView);
	}
}
